|
Groovy JDK |
Method Summary | |
---|---|
boolean
|
asBoolean()
Coerce an Object array to a boolean value. |
Object
|
asType(Class clazz)
Converts the given array to either a List, Set, or SortedSet. |
Map
|
collectEntries(Map collector, Closure transform)
Iterates through this array transforming each item using the transform closure
and returning a map of the resulting transformed entries.
|
Map
|
collectEntries(Map collector)
A variant of collectEntries using the identity closure as the transform. |
Map
|
collectEntries(Closure transform)
Iterates through this array transforming each item using the transform closure
and returning a map of the resulting transformed entries.
|
Map
|
collectEntries()
A variant of collectEntries using the identity closure as the transform. |
List
|
collectMany(Closure projection)
Projects each item from a source array to a collection and concatenates (flattens) the resulting collections into a single list. |
boolean
|
contains(Object value)
Checks whether the array contains the given value. |
Number
|
count(Object value)
Counts the number of occurrences of the given value inside this array. |
Number
|
count(Closure closure)
Counts the number of occurrences which satisfy the given closure from inside this array. |
Map
|
countBy(Closure closure)
Sorts all array members into groups determined by the supplied mapping closure and counts the group size. |
Object[]
|
drop(int num)
Drops the given number of elements from the head of this array if they are available. |
Object[]
|
dropWhile(Closure condition)
Create a suffix of the given array by dropping as many elements as possible from the front of the original array such that calling the given closure condition evaluates to true when passed each of the dropped elements. |
boolean
|
equals(List right)
Determines if the contents of this array are equal to the contents of the given list, in the same order. |
Object
|
find(Closure condition)
Finds the first element in the array that matches the given closure condition. |
Collection
|
findAll(Closure condition)
Finds all elements of the array matching the given Closure condition. |
Collection
|
findAll()
Finds the elements of the array matching the IDENTITY Closure (i.e. |
Object
|
first()
Returns the first item from the array. |
Collection
|
flatten()
Flatten an array. |
List
|
getAt(Collection indices)
Select a List of items from an Object array using a Collection to identify the indices to be selected. |
List
|
getAt(Range range)
Support the range subscript operator for an Array |
List
|
getAt(IntRange range)
|
List
|
getAt(EmptyRange range)
|
List
|
getAt(ObjectRange range)
|
Collection
|
grep(Object filter)
Iterates over the array of items and returns a collection of items that match the given filter - calling the Object#isCase
method used by switch statements.
|
Collection
|
grep()
Iterates over the array returning each element that matches using the IDENTITY Closure as a filter - effectively returning all elements which satisfy Groovy truth. |
Object
|
head()
Returns the first item from the Object array. |
Object
|
inject(Closure closure)
Iterates through the given array as with inject(Object[],initialValue,closure), but using the first element of the array as the initialValue, and then iterating the remaining elements of the array. |
Object
|
inject(Object initialValue, Closure closure)
Iterates through the given array, passing in the initial value to the closure along with the first item. |
Iterator
|
iterator()
Attempts to create an Iterator for the given object by first converting it to a Collection. |
String
|
join(String separator)
Concatenates the toString() representation of each
items in this array, with the given String as a separator between each
item.
|
Object
|
last()
Returns the last item from the array. |
Object
|
max()
Adds max() method to Object arrays. |
Object
|
max(Closure closure)
Selects the maximum value found from the Object array using the closure to determine the correct ordering. |
Object
|
max(Comparator comparator)
Selects the maximum value found from the Object array using the given comparator. |
Object
|
min()
Adds min() method to Object arrays. |
Object
|
min(Comparator comparator)
Selects the minimum value found from the Object array using the given comparator. |
Object
|
min(Closure closure)
Selects the minimum value found from the Object array using the closure to determine the correct ordering. |
Object[]
|
minus(Iterable removeMe)
Create an array composed of the elements of the first array minus the elements of the given Iterable. |
Object[]
|
minus(Object[] removeMe)
Create an array composed of the elements of the first array minus the elements of the given array. |
Object[]
|
minus(Object removeMe)
Create a new object array composed of the elements of the first array minus the element to remove. |
Object[]
|
plus(Object[] right)
Create an array as a union of two arrays. |
Object[]
|
plus(Object right)
Create an array containing elements from an original array plus an additional appended element. |
Object[]
|
plus(Collection right)
Create an array containing elements from an original array plus those from a Collection. |
Object[]
|
plus(Iterable right)
Create an array containing elements from an original array plus those from an Iterable. |
Object[]
|
reverse()
Creates a new array containing items which are the same as this array but in reverse order. |
Object[]
|
reverse(boolean mutate)
Reverse the items in an array. |
Object[]
|
reverseEach(Closure closure)
Iterate over each element of the array in the reverse order. |
int
|
size()
Provide the standard Groovy size() method for an array.
|
Object[]
|
sort()
Modifies this array so that its elements are in sorted order. |
Object[]
|
sort(boolean mutate)
Sorts the given array into sorted order. |
Object[]
|
sort(Comparator comparator)
Sorts the given array into sorted order using the given comparator. |
Object[]
|
sort(boolean mutate, Comparator comparator)
Modifies this array so that its elements are in sorted order as determined by the given comparator. |
Object[]
|
sort(Closure closure)
Sorts the elements from this array into a newly created array using the Closure to determine the correct ordering. |
Object[]
|
sort(boolean mutate, Closure closure)
Modifies this array so that its elements are in sorted order using the Closure to determine the correct ordering. |
Object
|
sum()
Sums the items in an array. |
Object
|
sum(Object initialValue)
Sums the items in an array, adding the result to some initial value. |
Object
|
sum(Closure closure)
Sums the result of apply a closure to each item of an array. |
Object
|
sum(Object initialValue, Closure closure)
Sums the result of applying a closure to each item of an array to some initial value. |
Object[]
|
tail()
Returns the items from the Object array excluding the first item. |
Object[]
|
take(int num)
Returns the first num elements from the head of this array.
|
Object[]
|
takeWhile(Closure condition)
Returns the longest prefix of this array where each element passed to the given closure evaluates to true. |
String
|
toArrayString()
Returns the string representation of the given array. |
List
|
toList()
Allows conversion of arrays into a mutable List. |
SpreadMap
|
toSpreadMap()
Creates a spreadable map from this array. |
String
|
toString()
Returns the string representation of this array's contents. |
Method Detail |
---|
public boolean asBoolean()
public Object asType(Class clazz)
clazz
- the desired class.public Map collectEntries(Map collector, Closure transform)
transform
closure
and returning a map of the resulting transformed entries.
def letters = "abc" def nums = [0, 1, 2] as Integer[] // collect letters with index assert nums.collectEntries( [:] ) { index -> [index, letters[index]] } == [0:'a', 1:'b', 2:'c'] assert nums.collectEntries( [4:'d'] ) { index -> [(index+1): letters[index]] } == [1:'a', 2:'b', 3:'c', 4:'d']
collector
- the Map into which the transformed entries are put.transform
- the closure used for transforming, which has an item from self as the parameter and
should return a Map.Entry, a Map or a two-element list containing the resulting key and value.public Map collectEntries(Map collector)
collector
- the Map into which the transformed entries are put.public Map collectEntries(Closure transform)
transform
closure
and returning a map of the resulting transformed entries.
def letters = "abc" def nums = [0, 1, 2] as Integer[] // collect letters with index using list style assert nums.collectEntries { index -> [index, letters[index]] } == [0:'a', 1:'b', 2:'c'] // collect letters with index using map style assert nums.collectEntries { index -> [(index): letters[index]] } == [0:'a', 1:'b', 2:'c']
transform
- the closure used for transforming, which has an item from self as the parameter and
should return a Map.Entry, a Map or a two-element list containing the resulting key and value.public Map collectEntries()
public List collectMany(Closure projection)
def nums = [1, 2, 3, 4, 5, 6] as Object[] def squaresAndCubesOfEvens = nums.collectMany{ it % 2 ? [] : [it**2, it**3] } assert squaresAndCubesOfEvens == [4, 8, 16, 64, 36, 216]
projection
- a projecting Closure returning a collection of items.public boolean contains(Object value)
value
- the value being searched for.public Number count(Object value)
compareTo(value) == 0
or equals(value)
).value
- the value being searched for.public Number count(Closure closure)
closure
- a closure condition.public Map countBy(Closure closure)
Example usage:
assert ([1,2,2,2,3] as Object[]).countBy{ it % 2 } == [1:2, 0:3]
closure
- a closure mapping items to the frequency keys.public Object[] drop(int num)
String[] strings = [ 'a', 'b', 'c' ] assert strings.drop( 0 ) == [ 'a', 'b', 'c' ] as String[] assert strings.drop( 2 ) == [ 'c' ] as String[] assert strings.drop( 5 ) == [] as String[]
num
- the number of elements to drop from this array.num
ones, or else the empty array, if this
array has less than num
elements.public Object[] dropWhile(Closure condition)
def nums = [ 1, 3, 2 ] as Integer[] assert nums.dropWhile{ it <= 3 } == [ ] as Integer[] assert nums.dropWhile{ it < 3 } == [ 3, 2 ] as Integer[] assert nums.dropWhile{ it != 2 } == [ 2 ] as Integer[] assert nums.dropWhile{ it == 0 } == [ 1, 3, 2 ] as Integer[]
condition
- the closure that must evaluate to true to
continue dropping elements.public boolean equals(List right)
false
if either collection is null
.right
- the List being compared.public Object find(Closure condition)
def list = [1,2,3] as Integer[] assert 2 == list.find { it > 1 } assert null == list.find { it > 5 }
condition
- a closure condition.public Collection findAll(Closure condition)
def items = [1,2,3,4] as Integer[] assert [2,4] == items.findAll { it % 2 == 0 }
condition
- a closure condition.public Collection findAll()
def items = [1, 2, 0, false, true, '', 'foo', [], [4, 5], null] as Object[] assert items.findAll() == [1, 2, true, 'foo', [4, 5]]
public Object first()
def array = [3, 4, 2].toArray() assert array.first() == 3
public Collection flatten()
public List getAt(Collection indices)
indices
- a Collection of indices.public List getAt(Range range)
range
- a Range.public List getAt(IntRange range)
range
- an IntRange.public List getAt(EmptyRange range)
range
- an EmptyRange.public List getAt(ObjectRange range)
range
- an ObjectRange.public Collection grep(Object filter)
Object#isCase
method used by switch statements. This method can be used with different
kinds of filters like regular expressions, classes, ranges etc.
Example:
def list = ['a', 'b', 'aa', 'bc', 3, 4.5] as Object[] assert list.grep( ~/a+/ ) == ['a', 'aa'] assert list.grep( ~/../ ) == ['aa', 'bc'] assert list.grep( Number ) == [ 3, 4.5 ] assert list.grep{ it.toString().size() == 1 } == [ 'a', 'b', 3 ]
filter
- the filter to perform on each element of the array (using the Object#isCase method).public Collection grep()
def items = [1, 2, 0, false, true, '', 'foo', [], [4, 5], null] as Object[] assert items.grep() == [1, 2, true, 'foo', [4, 5]]
public Object head()
def array = [3, 4, 2].toArray() assert array.head() == 3
public Object inject(Closure closure)
closure
- a closure.public Object inject(Object initialValue, Closure closure)
initialValue
- some initial value.closure
- a closure.public Iterator iterator()
public String join(String separator)
toString()
representation of each
items in this array, with the given String as a separator between each
item.separator
- a String separator.public Object last()
def array = [3, 4, 2].toArray() assert array.last() == 2
public Object max()
public Object max(Closure closure)
closure
- a Closure used to determine the correct ordering.public Object max(Comparator comparator)
comparator
- a Comparator.public Object min()
public Object min(Comparator comparator)
comparator
- a Comparator.public Object min(Closure closure)
closure
- a Closure used to determine the correct ordering.public Object[] minus(Iterable removeMe)
removeMe
- a Collection of elements to remove.public Object[] minus(Object[] removeMe)
removeMe
- an array of elements to remove.public Object[] minus(Object removeMe)
removeMe
- an element to remove from the array.public Object[] plus(Object[] right)
Integer[] a = [1, 2, 3] Integer[] b = [4, 5, 6] assert a + b == [1, 2, 3, 4, 5, 6] as Integer[]
right
- the right Array.public Object[] plus(Object right)
Integer[] a = [1, 2, 3] Integer[] result = a + 4 assert result == [1, 2, 3, 4] as Integer[]
right
- the value to append.public Object[] plus(Collection right)
Integer[] a = [1, 2, 3] def additions = [7, 8] assert a + additions == [1, 2, 3, 7, 8] as Integer[]
right
- a Collection to be appended.public Object[] plus(Iterable right)
class AbcIterable implements Iterable{ Iterator iterator() { "abc".iterator() } } String[] letters = ['x', 'y', 'z'] def result = letters + new AbcIterable() assert result == ['x', 'y', 'z', 'a', 'b', 'c'] as String[] assert result.class.array
right
- an Iterable to be appended.public Object[] reverse()
public Object[] reverse(boolean mutate)
mutate
- true if the array itself should be reversed in place and returned, false if a new array should be created.public Object[] reverseEach(Closure closure)
closure
- a closure to which each item is passed.public int size()
size()
method for an array.public Object[] sort()
public Object[] sort(boolean mutate)
def orig = ["hello","hi","Hey"] as String[] def sorted = orig.sort(false) assert orig == ["hello","hi","Hey"] as String[] assert sorted == ["Hey","hello","hi"] as String[] orig.sort(true) assert orig == ["Hey","hello","hi"] as String[]
mutate
- false will always cause a new array to be created, true will mutate the array in place.public Object[] sort(Comparator comparator)
comparator
- a Comparator used for the comparison.public Object[] sort(boolean mutate, Comparator comparator)
def orig = ["hello","hi","Hey"] as String[] def sorted = orig.sort(false, String.CASE_INSENSITIVE_ORDER) assert orig == ["hello","hi","Hey"] as String[] assert sorted == ["hello","Hey","hi"] as String[] orig.sort(true, String.CASE_INSENSITIVE_ORDER) assert orig == ["hello","Hey","hi"] as String[]
mutate
- false will always cause a new array to be created, true will mutate arrays in place.comparator
- a Comparator used for the comparison.public Object[] sort(Closure closure)
closure
- a Closure used to determine the correct ordering.public Object[] sort(boolean mutate, Closure closure)
def orig = ["hello","hi","Hey"] as String[] def sorted = orig.sort(false) { it.size() } assert orig == ["hello","hi","Hey"] as String[] assert sorted == ["hi","Hey","hello"] as String[] orig.sort(true) { it.size() } assert orig == ["hi","Hey","hello"] as String[]
mutate
- false will always cause a new array to be created, true will mutate arrays in place.closure
- a Closure used to determine the correct ordering.public Object sum()
public Object sum(Object initialValue)
initialValue
- the items in the array will be summed to this initial value.public Object sum(Closure closure)
array.sum(closure)
is equivalent to:
array.collect(closure).sum()
.closure
- a single parameter closure that returns a numeric value..public Object sum(Object initialValue, Closure closure)
array.sum(initVal, closure)
is equivalent to:
array.collect(closure).sum(initVal)
.closure
- a single parameter closure that returns a numeric value..initialValue
- the closure results will be summed to this initial value.public Object[] tail()
String[] strings = ["a", "b", "c"] def result = strings.tail() assert strings.class.componentType == String
public Object[] take(int num)
num
elements from the head of this array.
String[] strings = [ 'a', 'b', 'c' ] assert strings.take( 0 ) == [] as String[] assert strings.take( 2 ) == [ 'a', 'b' ] as String[] assert strings.take( 5 ) == [ 'a', 'b', 'c' ] as String[]
num
- the number of elements to take from this array.num
elements of this array,
or else the whole array if it has less then num
elements.public Object[] takeWhile(Closure condition)
def nums = [ 1, 3, 2 ] as Integer[] assert nums.takeWhile{ it < 1 } == [] as Integer[] assert nums.takeWhile{ it < 3 } == [ 1 ] as Integer[] assert nums.takeWhile{ it < 4 } == [ 1, 3, 2 ] as Integer[]
condition
- the closure that must evaluate to true to
continue taking elements.public String toArrayString()
{1, 2, "a"}
.public List toList()
public SpreadMap toSpreadMap()
public String toString()
|
Groovy JDK |